package com.zzyl.nursing.controller.member;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController{

@Autowired
private IReservationService IReservationservice;


@GetMapping("/cancelled-count")
@ApiOperation("查询取消预约数量")
public R<Integer> getCancelledReservationCount() {
    Long userId = UserThreadLocal.getUserId();
    System.out.println(userId);
    long time = System.currentTimeMillis();
    LocalDateTime ldt = LocalDateTimeUtil.of(time);
    LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
    LocalDateTime endTime = startTime.plusDays(1);
    if (IReservationservice.getCancelledReservationCount(startTime, endTime) > 0) {
        return R.ok(1);
    }
    return R.ok(0);
}

@PostMapping
@ApiOperation("新增预约信息")
public R<Reservation> addReservation(@RequestBody Reservation reservation) {
    IReservationservice.insertReservation(reservation);
    return  R.ok(reservation);
}

@GetMapping("page")
@ApiOperation("分页查询预约信息")
public R<TableDataInfo<List>> page(Reservation reservation) {
    startPage();
    List<Reservation> list = IReservationservice.selectReservationList(reservation);
    return  R.ok(getDataTable(list));
}

@GetMapping("countByTime")
@ApiOperation("查询每个时间剩余预约次数")
public R<Integer> countByTime() {
    return R.ok(0);
}

@PutMapping("/{id}/cancel")
@ApiOperation("取消预约")
public R<Reservation> cancelReservation(@PathVariable("id") Long id) {
    IReservationservice.deleteReservationById(id);
    return R.ok();
}
}